package com.jcp.baselibrary.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.jcp.baselibrary.R;

/**
 * Created by jzxiang on 16/4/19.
 */
public class CustomDialog extends DialogFragment {

    protected CustomDialogConfig mCustomDialogConfig;

    private static CustomDialog newInstance(CustomDialogConfig customDialogConfig) {
        CustomDialog dialog = new CustomDialog();
        dialog.initialize(customDialogConfig);
        return dialog;
    }

    protected void initialize(CustomDialogConfig customDialogConfig) {
        mCustomDialogConfig = customDialogConfig;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Activity activity = getActivity();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setCancelable(mCustomDialogConfig.isCancelable());
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getDialog() == null) {
            return;
        }
        Window window = getDialog().getWindow();
        if (window != null) {
            if (mCustomDialogConfig.getGravity() == Gravity.BOTTOM || mCustomDialogConfig.getGravity() == Gravity.TOP) {
                window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);//Here!
            } else {
                window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);//Here!
            }
            window.setGravity(mCustomDialogConfig.getGravity());
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return mCustomDialogConfig.getCustomView();
    }

    public int show(FragmentManager manager) {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(this, this.getClass().getSimpleName());
        return transaction.commitAllowingStateLoss();
    }

    public static class Builder {
        CustomDialogConfig mDialogConfig;

        public Builder() {
            mDialogConfig = new CustomDialogConfig();
        }

        public Builder setCustomView(View customView) {
            mDialogConfig.setCustomView(customView);
            return this;
        }

        public Builder isCancelable(boolean isCancelable) {
            mDialogConfig.setCancelable(isCancelable);
            return this;
        }

        public Builder setGravity(int gravity) {
            mDialogConfig.setGravity(gravity);
            return this;
        }

        public CustomDialog build() {
            return newInstance(mDialogConfig);
        }

    }
}
